package cn.zt.util;/**
 * Created by mi on 2018/10/8.
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author zt
 * @descrption
 * @date 2020/5/28
 **/
@Component
public class GuidUtil {
    public static String domainUrl;

    public static String sendmsgUrl;



    public static String getGuid(){
        StringBuffer now = new StringBuffer(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        int n = (int)(Math.random() * 900.0D);
        return now.append(n).toString();
    }

    public static String getZcbhid(){
        StringBuffer now = new StringBuffer(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        int n = (int)(Math.random() * 9000.0D + 10000.0D);
        return now.append(n).toString();
    }

    public static String longTimeToDay(Long ms){
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        return sb.toString();
    }

    /**
     * 将两个时间段的时间差转化
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static String getTime(String startTime,String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = sdf.parse(startTime);
        Date end = sdf.parse(endTime);

        Long startLong = start.getTime();
        Long endLong = end.getTime();
        Long ms = endLong-startLong;
        String time = longTimeToDay(ms);

        return time;
    }

    /**
     * 日期加减
     * @param num
     * @param newDate
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num,String newDate) throws ParseException{
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = sdf.parse(newDate);
        calendar.setTime(date);
        calendar.add(calendar.HOUR,num);
        date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 去重方法
     * @param list
     * @return
     */
    public static List removeDuplicate(List list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    public static void main(String[]args) throws ParseException {
//        String str = GuidUtil.getTime("2018-01-01 10:30:30","2018-01-01 12:30:40");
//        System.out.print(str);
    }

}
